/**
 * related to handlepage.ui
 * 
 * @Author : mafeng
 * @Timestamp : 2017-11-13
 */
//创建一个album实例
var album = sm("do_Album");
var util=require("ui/util");
util.init("do_ALayout_back");
var getdate = require("getdate")
var datetime_picker = sm("do_DateTimePicker")
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var Algorithm=sm("do_Algorithm");
var do_Notification = sm("do_Notification");
var do_Global = sm("do_Global");
var do_DataCache = sm("do_DataCache");

var imageBrowser = sm("do_ImageBrowser");
var sign_imageview = ui("do_ImageView_4");

var global_pic_array;

var grid_view = ui("do_GridView_1");
var temp_data = [ 
            {
            	id : "redian",
                "image": "",
            	template : 1  //采用的模板
            }
            ];

//定义do_SegmentView_tabs的数据Model
var grid_data = mm("do_ListData");
grid_data.addData(temp_data);
grid_view.bindItems(grid_data);
grid_view.refreshItems({});

//存储数据
do_Global.setMemory("pic_num", grid_data.getCount());

//在当前页面下订阅selectOneTab的事件
do_Page.on("selectPicture", function(data){
	deviceone.print(data);
	deviceone.print(JSON.stringify(data));
	for(var i=0; i<data.length;i++) {
		grid_data.addOne( {
			id : "redian",
            "image": data[i],
        	template : 0  //采用的模板
		},i);
	 global_pic_array = data;
	}
    for (var i = 0; i < data.length; i++) {
		
	Algorithm.base64("encode", "file", data[i], function(datae, e) {
		//do_Notification.toast("base64的方式编码文件");
		deviceone.print(datae)
		// 上传图片
		var http2 = mm("do_Http");
		http2.method = "POST"; // GET | POST
		http2.timeout = 10000; // 超时时间 : 单位 毫秒
		http2.contentType = "application/x-www-form-urlencoded"; // Content-Type
		http2.url = encodeURI(baseUrl + "method=public_addOneFj&psw=123");
		deviceone.print(http2.url)
		str = datae;
		var new_str = str.replace(/\+/g, "%2B");
		deviceone.print(new_str)
		http2.body = "dbTypeName=进度汇报附件&keyid="+ Keyid +"&fjType=img64&FjTrueName="+ Keyid +"&fjRemarks=&fjStr=" + new_str + "&userid="+ do_DataCache.loadData("user_id") +"&dbid=0";
		http2.on("success", function(dataa) {
			if (dataa["result"] == "1") {
				do_Notification.toast(dataa["msg"])	
			} else {
				do_Notification.toast(dataa["msg"])
			}
			deviceone.print(JSON.stringify(dataa));
			
		});
		http2.on("fail", function(dataa) {
			
			deviceone.print("fail")
			
		});
		//http2.request();
		});
    }
	
	deviceone.print(JSON.stringify(grid_data))
	do_Global.setMemory("pic_num", grid_data.getCount());
	deviceone.print(grid_data.getCount())
	if(grid_data.getCount() == 4) {
		grid_data.removeRange(3, 4);
	}
	grid_view.bindItems(grid_data);
	grid_view.refreshItems({});
	 
});

var getdata = do_Page.getData();

var onedata = getdata.maindata;

deviceone.print(getdata);

ui("do_Label_3").text = onedata.Man_name;
ui("do_Label_4").text = onedata.Phone;
ui("do_Label_5").text = onedata.All_name;

//grid_view.on("touch", function (data) {
//	var images = [];
//	
//	for (var i = 0; i < global_pic _array.length; i++) {
//		// push
//		images.push({source: global_pic_array[i]});
//		deviceone.print(images[i]["source"])
//	}
//	
//	imageBrowser.show(images, data);
//	
//})

// 签字
ui("do_Button_1").on("touch", function (data) {
	
	do_App.openPage({
		source: "source://view/todoReceipt/paint/index.ui",
		statusBarState: "transparent",
		animationType: "push_r2l"
	});
	
});

do_Page.on("result", function (data) {
	deviceone.print(data.image);
	sign_imageview.source = data.image;
})

ui("do_Button_3").on("touch", function() {
	datetime_picker.show({type: "1", title: "日期选择"}, function (data) {
		deviceone.print(JSON.stringify(data))
		ui("do_Button_3").text = getdate.date(data.time);
		//ui("do_Button_1").text = getdate.date('1350052653');
	})
})
